package algorithm;

import java.math.BigDecimal;

public class T00067 {
    public static void main(String[] args) {
        System.out.println(new Solution().addBinary("11", "1"));
    }

    static class Solution {
        public String addBinary(String a, String b) {
            int inc = 0;
            int i = a.length() - 1, j = b.length() - 1;
            StringBuilder sb = new StringBuilder();
            while (i >= 0 || j >= 0 || inc > 0) {
                if (i >= 0) {
                    inc += c2i(a.charAt(i));
                    i --;
                }
                if (j >= 0) {
                    inc += c2i(b.charAt(j));
                    j --;
                }
                if (inc >= 2) {
                    sb.append(i2c(inc - 2));
                    inc = 1;
                } else {
                    sb.append(i2c(inc));
                    inc = 0;
                }
            }
            return sb.reverse().toString();
        }

        public char i2c(int i) {
            if (i == 0) return '0';
            else return '1';
        }

        public int c2i(char c) {
            if (c == '0') return 0;
            else return 1;
        }
    }
}
